<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="handleInput"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          node-key="id"
          :data="depts"
          :props="defaultProps"
          default-expand-all
          highlight-current
          :expand-on-click-node="false"
          @current-change="handleCurrentChange"
        />
      </div>

      <div class="right">
        <!-- flex布局必须要加上,不写的话不会开启flex布局,默认是老的float布局 -->
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>

          <!--
            excel导入:   1.点击按钮 弹出对话框(强度)    2. 点击下载模板(下载得到一个文件=>你会做)  3.填写文件(会做)   4.点击上传 批量新增完成
          -->
          <el-button size="mini" @click="showDialog=true">excel导入</el-button>

          <el-button size="mini" :loading="loading" @click="handleExport">excel导出</el-button>

        </el-row>

        <!-- 表格组件 -->
        <el-table :data="rows">
          <el-table-column align="center" label="头像">
            <template #default="{ row }">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" />
              <span v-else class="hhh">{{ row.username.slice(2,3) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="手机号" sortable prop="mobile" />
          <el-table-column label="工号" sortable prop="workNumber" />
          <el-table-column label="聘用形式">
            <template #default="{ row }">
              <span>{{ row.formOfEmployment === 1? '正式':'🐂🐎' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry" />
          <el-table-column label="操作" width="280px">
            <template>
              <el-button size="mini" type="text" @click="$router.push('/employee/detail/${row.id}')">查看</el-button>

              <el-button size="mini" type="text">角色</el-button>

              <el-button size="mini" type="text">删除</el-button>

            </template>

          </el-table-column>

        </el-table>

        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            layout="total,prev, pager, next"
            :total="total"
            :current-page="page"
            @current-change="handlePageChange"
          />
        </el-row>
      </div>
      <!-- 对话框(不好维护 封装出去 => 组件通信) value   input -->
      <ImportExcel v-model="showDialog" @refresh="handleChange" />
      <!-- <ImportExcel :value="showDialog" @input="showDialog=$event" /> -->

    </div>

  </div>

</template>

<script>
import { getDepartments } from '@/api/department'
import fileSaver from 'file-saver'
import { transList2TreeData } from '@/utils'
import { getEmployeeList, getFileData } from '@/api/employee'
import ImportExcel from './components/import-excel.vue'

export default {
  name: 'Employee',
  components: {
    ImportExcel
  },
  data() {
    return {
      showDialog: false,
      loading: false,
      depts: [], // 组织数据

      selectId: null, // 选中的部门id
      tatal: 0, // 总条数
      rows: [], // 员工列表
      page: 1, // 当前页码
      keyword: '', // 搜索关键字
      // timer: null, //定时器不需要响应式
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  created() {
    this.getDepts()
  },
  methods: {

    async  handleExport() {
      try {
        this.loading = true
        const res = await getFileData()
        this.loading = false
        console.log(res)
        fileSaver.saveAs(res, '员工信息.xlsx')
      } catch {
        this.$message.error('文件获取失败')
      } finally {
        this.loading = false
      }
    },

    handlePageChange(val) {
      this.page = val
      this.getEmployeeList()
    },
    async getDepts() {
      const res = await getDepartments()
      // console.log(res)
      this.depts = transList2TreeData(res, 0) // 将处理好的数据赋值给depts 然后tree组件就立即渲染出来了(等会出来的)
      // 一进入页面 用户不选择部门时 也需要默认获取传智教育的所有员工进行展示
      // 一旦获取数据 立即将传智教育的id存下来
      this.selectId = this.depts[0].id
      // 等tree渲染完成后 才能高亮
      this.$nextTick(() => {
        // 高亮传智教育
        this.$refs.tree.setCurrentKey(this.selectId)
      })
      this.getEmployeeList() // 获取员工列表
    },

    handleCurrentChange(obj) {
      console.log('有人选了一个节点', obj)
      // 记录id 将来配合右侧进行数据展示
      this.selectId = obj.id
      this.getEmployeeList() // 获取选中部门下的员工数据

      this.page = 1 // 一旦切换部门 页码就回到第一页
    },

    async getEmployeeList() {
      // 获取员工列表
      const { total, rows } = await getEmployeeList({
        page: this.page,
        pagesize: 5,
        departmentId: this.selectId,
        keyword: this.keyword
      })
      this.total = total
      this.rows = rows
    },

    handleInput() {
      // 防抖
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        // 在请求之前重置页码
        this.page = 1
        // 一旦输入内容就触发
        this.getEmployeeList()
      }, 500)
    }

  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
  .hhh {
  display: inline-block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #8df;

  text-align: center;
  line-height: 40px;
  color: #fff;
  font-size: 20px;
}
}

</style>
